package com.netease.yunxin.kit.chatkit.ui.share

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.impl.FullScreenPopupView
import com.netease.yunxin.kit.chatkit.ui.R
import java.io.File

class ShareFileDialog(context: Context) : CenterPopupView(context) {
    private lateinit var recyclerView: RecyclerView
    private val adapter by lazy { ShareFileAdapter() }
    var confirmListener: ((String?) -> Unit)? = null
    override fun getImplLayoutId(): Int = R.layout.activity_share_file

    override fun onCreate() {
        super.onCreate()
        recyclerView = findViewById(R.id.recyclerView)
        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(context)
        val desPath = context.externalCacheDir.toString() + File.separator + "salton"
        val dataList = File(desPath)?.listFiles()?.map { it.absolutePath }?.toMutableList()
        if (dataList != null) {
            adapter.setData(dataList)
        }
        adapter.itemClick = {
            confirmListener?.invoke(it)
            dismiss()
        }
    }

    companion object {
        private const val TAG = "ShareFileDialog"
        fun show(activity: Context, confirmListener: ((String?) -> Unit)?) {
            val contentView = ShareFileDialog(activity)
            contentView.confirmListener = confirmListener
            XPopup.Builder(activity).dismissOnTouchOutside(false).dismissOnBackPressed(true)
                .autoDismiss(false).hasShadowBg(true).isLightStatusBar(true).asCustom(contentView)
                .show()
        }
    }


}